package vip.xiaonuo.supplier.modular.custom.objects.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PinyinUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.custom.objects.entity.SupplierObjects;
import vip.xiaonuo.supplier.modular.custom.objects.mapper.SupplierObjectsMapper;
import vip.xiaonuo.supplier.modular.custom.objects.param.SupplierObjectsAddParam;
import vip.xiaonuo.supplier.modular.custom.objects.param.SupplierObjectsEditParam;
import vip.xiaonuo.supplier.modular.custom.objects.param.SupplierObjectsIdParam;
import vip.xiaonuo.supplier.modular.custom.objects.param.SupplierObjectsPageParam;
import vip.xiaonuo.supplier.modular.custom.objects.service.SupplierObjectsService;
import vip.xiaonuo.supplier.modular.purchase.suppliers.entity.SupplierSuppliers;

import java.util.List;

/**
 * 业务对象Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/04/19 11:37
 **/
@Service
public class SupplierObjectsServiceImpl extends ServiceImpl<SupplierObjectsMapper, SupplierObjects> implements SupplierObjectsService {

    @Override
    public Page<SupplierObjects> page(SupplierObjectsPageParam supplierObjectsPageParam) {
        QueryWrapper<SupplierObjects> queryWrapper = new QueryWrapper<SupplierObjects>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(supplierObjectsPageParam.getObjectCode())) {
            queryWrapper.lambda().eq(SupplierObjects::getObjectCode, supplierObjectsPageParam.getObjectCode());
        }
        if(ObjectUtil.isNotEmpty(supplierObjectsPageParam.getObjectType())) {
            queryWrapper.lambda().eq(SupplierObjects::getObjectType, supplierObjectsPageParam.getObjectType());
        }
        if(ObjectUtil.isNotEmpty(supplierObjectsPageParam.getObjectName())) {
            queryWrapper.lambda().like(SupplierObjects::getObjectName, supplierObjectsPageParam.getObjectName());
        }
        if(ObjectUtil.isNotEmpty(supplierObjectsPageParam.getObjectPerson())) {
            queryWrapper.lambda().like(SupplierObjects::getObjectPerson, supplierObjectsPageParam.getObjectPerson());
        }
        if(ObjectUtil.isNotEmpty(supplierObjectsPageParam.getObjectContact())) {
            queryWrapper.lambda().like(SupplierObjects::getObjectContact, supplierObjectsPageParam.getObjectContact());
        }
        if(ObjectUtil.isNotEmpty(supplierObjectsPageParam.getObjectTel())) {
            queryWrapper.lambda().like(SupplierObjects::getObjectTel, supplierObjectsPageParam.getObjectTel());
        }
        if(ObjectUtil.isNotEmpty(supplierObjectsPageParam.getObjectAllowCredit())) {
            queryWrapper.lambda().eq(SupplierObjects::getObjectAllowCredit, supplierObjectsPageParam.getObjectAllowCredit());
        }
        if(ObjectUtil.isNotEmpty(supplierObjectsPageParam.getObjectStoreId())) {
            queryWrapper.lambda().eq(SupplierObjects::getObjectStoreId, supplierObjectsPageParam.getObjectStoreId());
        }
        if(ObjectUtil.isNotEmpty(supplierObjectsPageParam.getObjectStatus())) {
            queryWrapper.lambda().eq(SupplierObjects::getObjectStatus, supplierObjectsPageParam.getObjectStatus());
        }
        if(ObjectUtil.isNotEmpty(supplierObjectsPageParam.getSearchKey())) {
            queryWrapper.and(q -> q.lambda().like(SupplierObjects::getObjectCode, supplierObjectsPageParam.getSearchKey())
                    .or().like(SupplierObjects::getObjectName, supplierObjectsPageParam.getSearchKey())
                    .or().like(SupplierObjects::getObjectPinyin, supplierObjectsPageParam.getSearchKey())
                    .or().like(SupplierObjects::getObjectPerson, supplierObjectsPageParam.getSearchKey())
                    .or().like(SupplierObjects::getObjectContact, supplierObjectsPageParam.getSearchKey())
                    .or().like(SupplierObjects::getObjectTel, supplierObjectsPageParam.getSearchKey())
                    .or().like(SupplierObjects::getObjectAddress, supplierObjectsPageParam.getSearchKey())
            );
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierObjects::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierObjectsPageParam.getSortField(), supplierObjectsPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierObjectsPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierObjectsPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierObjectsPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierObjects::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierObjectsAddParam supplierObjectsAddParam) {
        SupplierObjects supplierObjects = BeanUtil.toBean(supplierObjectsAddParam, SupplierObjects.class);
        supplierObjects.setObjectPinyin(PinyinUtil.getAllFirstLetter(supplierObjects.getObjectName()));
        //数据归属
        supplierObjects.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierObjects);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierObjectsEditParam supplierObjectsEditParam) {
        SupplierObjects supplierObjects = this.queryEntity(supplierObjectsEditParam.getId());
        BeanUtil.copyProperties(supplierObjectsEditParam, supplierObjects);
        supplierObjects.setObjectPinyin(PinyinUtil.getAllFirstLetter(supplierObjects.getObjectName()));

        if(ObjectUtil.isNotEmpty(supplierObjects.getObjectReference()) && supplierObjects.getObjectReference().equals(supplierObjects.getId())) {
            throw new CommonException("推荐人不能是自己");
        }
        this.updateById(supplierObjects);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierObjectsIdParam> supplierObjectsIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierObjectsIdParamList, SupplierObjectsIdParam::getId));
    }

    @Override
    public SupplierObjects detail(SupplierObjectsIdParam supplierObjectsIdParam) {
        return this.queryEntity(supplierObjectsIdParam.getId());
    }

    @Override
    public SupplierObjects queryEntity(String id) {
        SupplierObjects supplierObjects = this.getById(id);
        if(ObjectUtil.isEmpty(supplierObjects)) {
            throw new CommonException("业务对象不存在，id值为：{}", id);
        }
        return supplierObjects;
    }
}
